package com.cl.code.web.common.model;

import lombok.Data;

/**
 * 统一API响应结果封装
 *
 * @author chengliang
 * @since 1.0.0
 */
@Data
public class ApiResult<T> {
    private int code;
    private String message;
    private T data;

    private ApiResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> ApiResult<T> success(T data) {
        return new ApiResult<>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMessage(), data);
    }

    public static <T> ApiResult<T> success(String message, T data) {
        return new ApiResult<>(ResultEnum.SUCCESS.getCode(), message, data);
    }

    public static <T> ApiResult<T> of(Result result) {
        return new ApiResult<>(result.getCode(), result.getMessage(), null);
    }

    public static <T> ApiResult<T> of(Result result, T data) {
        return new ApiResult<>(result.getCode(), result.getMessage(), data);
    }

    public static ApiResult<Void> error(int code, String message) {
        return new ApiResult<>(code, message, null);
    }

    public static ApiResult<Void> error(String message) {
        return new ApiResult<>(ResultEnum.INTERNAL_SERVER_ERROR.getCode(), message, null);
    }

    public static ApiResult<Void> error(Result result) {
        return new ApiResult<>(result.getCode(), result.getMessage(), null);
    }

    public static ApiResult<Void> empty() {
        return success(null);
    }

}